package com.kmxd.ams.app.archive;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.archive.IArchiveSealSvc;
import com.kmxd.ams.client.archive.dto.ArchiveSealAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveSealEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveSealQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveSealListVO;
import com.kmxd.ams.client.archive.vo.ArchiveSealVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveSeal;
import com.kmxd.ams.infra.archive.mapper.ArchiveSealMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 档号章设置 服务实现类
 *
 * @author ZuoLG
 * @since 2023-09-11
 */
@Service
@AllArgsConstructor
public class ArchiveSealSvcImpl extends ServiceImpl<ArchiveSealMapper, ArchiveSeal>
    implements IArchiveSealSvc {

  @Override
  public Page<ArchiveSealListVO> selectPage(Page reqPage, ArchiveSealQueryDTO req) {
    LambdaQueryWrapper<ArchiveSeal> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveSeal>(), req).lambda();
    Page<ArchiveSeal> page = baseMapper.selectPage(reqPage, queryWrapper);
    Page<ArchiveSealListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ArchiveSealListVO.class));
    return voPage;
  }

  @Override
  public List<ArchiveSealListVO> selectList(ArchiveSealQueryDTO req) {
    LambdaQueryWrapper<ArchiveSeal> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveSeal>(), req).lambda();
    List<ArchiveSeal> list = baseMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveSealListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ArchiveSealAddDTO req) {
    ArchiveSeal entity = BeanUtil.copyProperties(req, ArchiveSeal.class);
    baseMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ArchiveSealEditDTO req) {
    ArchiveSeal entity = BeanUtil.copyProperties(req, ArchiveSeal.class);
    baseMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public ArchiveSealVO view(Long id) {
    ArchiveSeal entity = baseMapper.selectById(id);
    return BeanUtil.copyProperties(entity, ArchiveSealVO.class);
  }
}
